package cn.gzjp.modules.woms.service;

import java.util.List;

import cn.gzjp.modules.sys.dao.UserDao;
import cn.gzjp.modules.sys.entity.Office;
import cn.gzjp.modules.sys.entity.Role;
import cn.gzjp.modules.sys.entity.User;
import cn.gzjp.modules.sys.service.SystemService;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.modules.woms.entity.Worker;
import cn.gzjp.modules.woms.dao.WorkerDao;


/**
 * 工作人员Service
 * @author liyp
 * @version 2018-07-07
 */
@Service
@Transactional(readOnly = true)
public class WorkerService extends CrudService<WorkerDao, Worker> {

	@Autowired
	private SystemService systemService;
	@Autowired
	private UserDao userDao;
	
	public Worker get(String id) {
		Worker worker = super.get(id);
		return worker;
	}
	
	public List<Worker> findList(Worker worker) {
		return super.findList(worker);
	}
	
	public Page<Worker> findPage(Page<Worker> page, Worker worker) {
		return super.findPage(page, worker);
	}
	
	@Transactional(readOnly = false)
	public void save(Worker worker) {
		User user = new User();
		user.setCompany(new Office("1"));
		user.setOffice(new Office("5"));
		user.setUserType("1");
		user.setLoginName(worker.getMobile());
		user.setMobile(worker.getMobile());
		user.setName(worker.getName());
		user.setPassword(SystemService.entryptPassword(worker.getMobile()));
		if(StrUtil.isNotBlank(worker.getPassword())){
			user.setPassword(SystemService.entryptPassword(worker.getPassword()));
		}
		List<Role> roleList = Lists.newArrayList();
		roleList.add(new Role("2"));
		user.setRoleList(roleList);
		User existUser = userDao.getByLoginName(user);
		if(existUser==null){
			systemService.saveUser(user);//不存在，创建
		}
		if(existUser!=null&& StrUtil.isNotBlank(worker.getPassword())){//更新密码
			existUser.setCompany(new Office("1"));
			existUser.setOffice(new Office("5"));
			existUser.setUserType("1");
			existUser.setLoginName(worker.getMobile());
			existUser.setMobile(worker.getMobile());
			existUser.setName(worker.getName());
			existUser.setPassword(SystemService.entryptPassword(worker.getPassword()));
			roleList = Lists.newArrayList();
			roleList.add(new Role("2"));
			existUser.setRoleList(roleList);
			systemService.saveUser(existUser);
		}
		super.save(worker);
	}
	
	@Transactional(readOnly = false)
	public void delete(Worker worker) {
		super.delete(worker);
	}


	@Transactional(readOnly = false)
	public void save(Worker worker,String id) {
		User user = new User();
		user.setCompany(new Office("1"));
		user.setOffice(new Office("5"));
		user.setUserType("1");
		user.setLoginName(worker.getMobile());
		user.setMobile(worker.getMobile());
		user.setName(worker.getName());
		user.setPassword(SystemService.entryptPassword(worker.getMobile()));
		List<Role> roleList = Lists.newArrayList();
		roleList.add(new Role("2"));
		user.setRoleList(roleList);
		User existUser = userDao.getByLoginName(user);//不存在，创建
		if(existUser==null){
			systemService.saveUser(user);
		}
		super.insert(worker,id);
	}

	public Worker findUniqueByMobile(String mobile) {
		return super.findUniqueByProperty("mobile", mobile);
	}


}